package com.haitonghop.main;

import vn.clevernet.android.sdk.ClevernetView;
import vn.clevernet.android.sdk.ClevernetView.ClevernetViewCallbackListener;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.google.ads.AdSize;
import com.google.ads.mediation.MediationAdRequest;
import com.google.ads.mediation.customevent.CustomEventBanner;
import com.google.ads.mediation.customevent.CustomEventBannerListener;
import com.haitonghop.R;

public class CustomAd implements CustomEventBanner {
	static final String TAG = "CleverNetAds";

	@Override
	public void requestBannerAd(final CustomEventBannerListener listener,
	                              final Activity activity,
	                              String label,
	                              String serverParameter,
	                              AdSize adSize,
	                              MediationAdRequest request,
	                              Object customEventExtra) {

        // set the callback listener, to receive a message when an ad was loaded
        ClevernetView.enableCustomEvent();

        View v = activity.getLayoutInflater().inflate(R.layout.clevernetadslayout, null);

        final ClevernetView cleverNetView = (ClevernetView) v.findViewById(R.id.cadad);

        if(cleverNetView.getParent() != null){
        	((ViewGroup) cleverNetView.getParent()).removeView(cleverNetView);
         }

        Log.d(TAG, "Loading clever net ads");

        cleverNetView.setCleverNetViewCallbackListener(new ClevernetViewCallbackListener() {

			@Override
			public void onLoaded(boolean arg0, ClevernetView arg1) {
				// TODO Auto-generated method stub
				Log.d(TAG, "Loaded clever net ads");
				listener.onReceivedAd(cleverNetView);
			}

			@Override
			public void onIllegalHttpStatusCode(int arg0, String arg1) {
				// TODO Auto-generated method stub
				listener.onFailedToReceiveAd();
				Log.d(TAG, "Failed to load clever net ads due to illegal http status code.");
			}

			@Override
			public void onError(Exception arg0) {
				// TODO Auto-generated method stub
				listener.onFailedToReceiveAd();
				Log.d(TAG, "Failed to load clever net ads due to errors.");
				throw new RuntimeException(arg0);
			}
		});
	}

	 @Override
	  public void destroy() {
	  }

}
